package com.peach.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peach.pojo.Activity;
import com.peach.pojo.User;
import com.peach.pojo.UserBranch;
import com.peach.pojo.UserStage;
import com.peach.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/selectAllUsers")
    public String selectAllUsersByConditions(Model model,
                                             Integer userId,
                                             String userName,
                                             String userClass,
                                             Integer userBranchId,
                                             Integer userStageId,
                                             @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                             @RequestParam(defaultValue="5",value="pageSize")Integer pageSize){

        //为了程序的严谨性，判断非空：
        if(pageNum == null){
            //设置默认当前页
            pageNum = 1;
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            //设置默认每页显示的数据数
            pageSize = 5;
        }

        PageHelper.startPage(pageNum,pageSize);

        try {
            List<User> users = userService.selectAllUsersByConditions(userId,userName,
                    userClass,userBranchId,userStageId);
            model.addAttribute("users",users);

            List<UserBranch> userBranches = userService.selectAllUserBranches();
            List<UserStage> userStages = userService.selectAllUserStages();
            model.addAttribute("userBranches",userBranches);
            model.addAttribute("userStages",userStages);

            model.addAttribute("userId",userId);
            model.addAttribute("userName",userName);
            model.addAttribute("userClass",userClass);
            model.addAttribute("userBranchId",userBranchId);
            model.addAttribute("userStageId",userStageId);

            PageInfo<User> pageInfo = new PageInfo<User>(users,pageSize);
            model.addAttribute("pageInfo",pageInfo);
        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "/user/user_list";
    }

    @GetMapping("/addOneUser")
    public String addOneUser(Model model){
        List<UserBranch> userBranches = userService.selectAllUserBranches();
        List<UserStage> userStages = userService.selectAllUserStages();
        model.addAttribute("userBranches",userBranches);
        model.addAttribute("userStages",userStages);
        return "/user/user_add";
    }

    @PostMapping("/toAddUser")
    public String toAddUser(User user,Integer userBranchId,Integer userStageId){
        UserBranch userBranch = new UserBranch(userBranchId);
        UserStage userStage = new UserStage(userStageId);
        user.setUserBranch(userBranch);
        user.setUserStage(userStage);
        userService.insertOneUser(user);
        return "redirect:/user/selectAllUsers";
    }

    @GetMapping("/updateUser")
    public String updateUser(Model model,Integer userId){
        User user = userService.selectUsersById(userId);
        List<UserBranch> userBranches = userService.selectAllUserBranches();
        List<UserStage> userStages = userService.selectAllUserStages();
        model.addAttribute("userBranches",userBranches);
        model.addAttribute("userStages",userStages);
        model.addAttribute("user",user);
        return "/user/user_update";
    }

    @PostMapping("/toUpdateUser")
    public String toUpdateUser(User user,Integer userBranchId,Integer userStageId){
        UserBranch userBranch = new UserBranch(userBranchId);
        UserStage userStage = new UserStage(userStageId);
        user.setUserBranch(userBranch);
        user.setUserStage(userStage);
        userService.updateOneUser(user);
        return "redirect:/user/selectAllUsers";
    }

    @GetMapping("/deleteOneUser")
    public String deleteUser(Integer userId){
        userService.deleteOneUser(userId);
        return "redirect:/user/selectAllUsers";
    }

    @GetMapping("/selectUsersByBranch")
    @ResponseBody
    public List<User> selectUsersByBranch(Integer userBranchId,Model model){
        List<User> users = userService.selectUsersByBranch(userBranchId);
        model.addAttribute("users",users);
        return users;
    }






}
